package jse.test20160413.serializableAndClone;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Test;

/**
 * @desc   测试序列化与克隆
 * @author kanggw
 * @datetime 2016年4月13日,下午8:47:59
 */
public class Tet {
	
	@Test
	public void test() {
		
		Adress adress = new Adress("内蒙古","赤峰","林夕");
		Employee employee = new Employee("康康", adress);
		System.out.println("序列化前 ============================");
		System.out.println(employee);
		System.out.println("序列化后============================");
		
		//通过输入和输出流把文件信息存放到本地
		ObjectOutputStream outputStream = null;
		ObjectInputStream inputStream = null;
		Employee employee2 = null;
		
		try {
			//建立输入文件名
			outputStream = new ObjectOutputStream(new FileOutputStream("employee.dat"));
			//写出
			outputStream.writeObject(employee);
			inputStream = new ObjectInputStream(new FileInputStream("employee.dat"));
			employee2 = (Employee)inputStream.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				inputStream.close();
				outputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != employee2) {
			employee2.setName("汪汪");
			employee2.getAdress().setState("北京");
			employee2.getAdress().setProvince("五环");
			employee2.getAdress().setCity("昌平");
		}
		System.out.println(employee);
		System.out.println(employee2);
		
		
	}
}
